﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ValidateDemo
{
    public partial class LoginFrm : Form
    {
        public LoginFrm()
        {
            InitializeComponent();

            this.txtUsername.Focus();
        }

        private void txtUsername_Validating(object sender, CancelEventArgs e)
        {
            if(string.IsNullOrEmpty(txtUsername.Text)) 
            {
                // e.Cancel = true;   // 设置e.Cancel = true 后，不输入用户名，窗口无法关闭
                txtUsername.Focus();
                errorProvider.SetError(txtUsername, "请输入用户名,");
            }
            else
            {
                //e.Cancel= false;
                errorProvider.SetError(txtUsername, null);
            }

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if(ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show(txtUsername.Text, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
